<?php
/**
 * inventory actions.
 *
 * @package    ubss
 * @subpackage inventory
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class inventoryActions extends sfActions
{
    public function executeSearch(sfWebRequest $request)
    {
        if (!$query = $request->getParameter('query'))
        {
            return $this->forward('inventory', 'index');
        }
        $this->bookCopies = Doctrine::getTable('Inventory')->getForLuceneQuery($query);
        
        if ($request->isXmlHttpRequest())
        {
            if ('*' == $query || !$this->bookCopies)
            {
                return $this->renderText('No results.');
            }
            else
            {
                return $this->renderPartial('inventory/list', array('bookCopies' => $this->bookCopies));
            }
        }
        
        
    }
    public function executeIndex(sfWebRequest $request)
    {
        //$this->books = Doctrine::getTable('Book')->getWithInventory();
        $this->pager = new sfDoctrinePager('Inventory', sfConfig::get('app_max_books_on_homepage'));
        $this->pager->setQuery(Doctrine::getTable('Book')->getWithInventory());
        $this->pager->setPage($request->getParameter('page', 1));
        $this->pager->init();
    }
    public function executeShow(sfWebRequest $request)
    {
        /*$this->inventory = Doctrine::getTable ( 'Inventory' )->find ( array ($request->getParameter ( 'id' ) ) );
		$this->forward404Unless ( $this->inventory );*/
        $this->inventory = $this->getRoute()->getObject();
        $this->getUser()->addBookCopyToHistory($this->inventory);
    }
    public function executeNew(sfWebRequest $request)
    {
        $this->form = new InventoryForm();
    }
    public function executeCreate(sfWebRequest $request)
    {
        $this->form = new InventoryForm();
        $this->processForm($request, $this->form);
        $this->setTemplate('new');
    }
    public function executeEdit(sfWebRequest $request)
    {
        $this->form = new InventoryForm($this->getRoute()->getObject());
    }
    public function executeUpdate(sfWebRequest $request)
    {
        $this->form = new InventoryForm($this->getRoute()->getObject());
        $this->processForm($request, $this->form);
        $this->setTemplate('edit');
    }
    public function executeDelete(sfWebRequest $request)
    {
        $request->checkCSRFProtection();
        $inventory = $this->getRoute()->getObject();
        $inventory->delete();
        $this->redirect('inventory/index');
    }
    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid())
        {
            $inventory = $form->save();
            $this->getUser()->setFlash('notice', sprintf('Votre livre a bien ete sauvegarde.'));
            $this->redirect('inventory_show', $inventory);
        }
    }
}
